package com.ruoyi.web.controller.his;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.his.domain.vo.PreVo;
import com.ruoyi.his.domain.vo.VisitQuery;
import com.ruoyi.his.domain.vo.VisitVo;
import com.ruoyi.system.domain.vo.HisHospitalNationVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HisHospitalNation;
import com.ruoyi.system.service.IHisHospitalNationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 国家机构Controller
 * 
 * @author Alan
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/system/nation")
public class HisHospitalNationController extends BaseController
{
    @Autowired
    private IHisHospitalNationService hisHospitalNationService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询国家机构列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HisHospitalNation hisHospitalNation)
    {
        startPage();
        List<HisHospitalNation> list = hisHospitalNationService.selectHisHospitalNationList(hisHospitalNation);
        return getDataTable(list);
    }

    @GetMapping("/lists")
    public TableDataInfo lists(HisHospitalNation hisHospitalNation)
    {
        startPage();
        List<HisHospitalNationVo> list = hisHospitalNationService.selectHisHospitalNationLists(hisHospitalNation);
        return getDataTable(list);
    }


    @GetMapping("/listVisit")
    public TableDataInfo listVisit(VisitQuery visitQuery)
    {
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }
        if(CollectionUtils.isNotEmpty(hospitalIds)){
            visitQuery.setHospitalIds(String.join(",", hospitalIds));
        }
        if(CollectionUtils.isNotEmpty(signatures)){
            visitQuery.setSignatures(String.join(",", signatures));
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        JSONObject map = hisHospitalNationService.selectListVisit(visitQuery, pageNum, pageSize);
        TableDataInfo dataTable = getDataTable(map.getJSONArray("list"));
        dataTable.setTotal(map.getLong("total"));
        return dataTable;
    }


    @GetMapping("/listPre")
    public TableDataInfo listPre(VisitQuery visitQuery)
    {
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }
        if(CollectionUtils.isNotEmpty(hospitalIds)){
            visitQuery.setHospitalIds(String.join(",", hospitalIds));
        }
        if(CollectionUtils.isNotEmpty(signatures)){
            visitQuery.setSignatures(String.join(",", signatures));
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        JSONObject map = hisHospitalNationService.selectListPre(visitQuery,pageNum,pageSize);
        TableDataInfo dataTable = getDataTable(map.getJSONArray("list"));
        dataTable.setTotal(map.getLong("total"));
        return dataTable;
    }

    @GetMapping("/{clinic_id}")
    public AjaxResult selectPre(@PathVariable("clinic_id") String clinic_id)
    {
        PreVo list = hisHospitalNationService.selectPre(clinic_id);
        return AjaxResult.success(list);
    }

    @GetMapping("/getinfo")
    public AjaxResult getinfo(@RequestParam("registerNumber") String registerNumber)
    {
        HisHospitalNation list = hisHospitalNationService.getinfo(registerNumber);
        return AjaxResult.success(list);
    }

    /**
     * 导出国家机构列表
     */
//    @PreAuthorize("@ss.hasPermi('system:nation:export')")
    @Log(title = "国家机构", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HisHospitalNation hisHospitalNation)
    {
        List<HisHospitalNation> list = hisHospitalNationService.selectHisHospitalNationList(hisHospitalNation);
        ExcelUtil<HisHospitalNation> util = new ExcelUtil<HisHospitalNation>(HisHospitalNation.class);
        return util.exportExcel(list, "国家机构数据");
    }

    /**
     * 新增国家机构
     */
//    @PreAuthorize("@ss.hasPermi('system:nation:add')")
    @Log(title = "国家机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HisHospitalNation hisHospitalNation)
    {
        return toAjax(hisHospitalNationService.insertHisHospitalNation(hisHospitalNation));
    }

    /**
     * 修改国家机构
     */
//    @PreAuthorize("@ss.hasPermi('system:nation:edit')")
    @Log(title = "国家机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HisHospitalNation hisHospitalNation)
    {
        return toAjax(hisHospitalNationService.updateHisHospitalNation(hisHospitalNation));
    }

    /**
     * 删除国家机构
     */
//    @PreAuthorize("@ss.hasPermi('system:nation:remove')")
    @Log(title = "国家机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uniqueCode}")
    public AjaxResult remove(@PathVariable Integer[] uniqueCodes)
    {
        return toAjax(hisHospitalNationService.deleteHisHospitalNationByUniqueCodes(uniqueCodes));
    }

}
